package com.lianxi.nmms.controller;

import com.github.pagehelper.PageInfo;
import com.lianxi.nmms.constant.ResponseCodeConstant;
import com.lianxi.nmms.entity.Dept;
import com.lianxi.nmms.entity.ProductType;
import com.lianxi.nmms.entity.Staff;
import com.lianxi.nmms.exception.*;
import com.lianxi.nmms.service.DeptService;
import com.lianxi.nmms.service.StaffService;
import com.lianxi.nmms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 翔哥
 * 2025年09月28日
 */
@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffService staffService;

    @Autowired
    private DeptService deptService;

    @RequestMapping("/login")
    public ModelAndView login(String loginName, String password, String role, String code, HttpSession session){
        ModelAndView mav =new ModelAndView();
        String image = (String) session.getAttribute("code");

        try {
            Staff staff =staffService.login(loginName, password, role, code, image);
            session.setAttribute("staff", staff);
            mav.setViewName("redirect:/showMain");
        } catch (RequestParameterErrorException e) {
            mav.addObject("staffMsg", e.getMessage());
            mav.setViewName("backend/login");
        } catch (CodeErrorException e) {
            mav.addObject("staffMsg", e.getMessage());
            mav.setViewName("backend/login");
        } catch (StaffNotExistException e) {
            mav.addObject("staffMsg", e.getMessage());
            mav.setViewName("backend/login");
        } catch (Exception e) {
            e.printStackTrace();
            mav.addObject("staffMsg", "服务器内部异常");
            mav.setViewName("backend/login");
        }
        return mav;
    }

    @RequestMapping("/findAll")
    public ModelAndView findAll(Staff staffAll,String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        PageInfo<Staff> staffPage = staffService.findPage(staffAll,pageNo, pageSize);
        List<Dept> allDept=deptService.findAll();
        List<Dept> notDept=deptService.findStatus();
        mav.addObject("allDept",allDept);
        mav.addObject("notDept",notDept);

        mav.addObject("staffPage", staffPage);
        mav.setViewName("backend/staffManager");

        mav.addObject("staffAll",staffAll);

        return mav;
    }

    @RequestMapping("/findStaffById")
    public ResponseResult findStaffById(String id){
        ResponseResult result =new ResponseResult();
        try {
            Staff staff =  staffService.findStaffById(id);
            result.setMessage("成功");
            result.setReturnObject(staff);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @RequestMapping("/addStaff")
    public ResponseResult addStaff(String name, String loginName , String password , String phone , String email, String role , String deptId,HttpSession session){
        ResponseResult result =new ResponseResult();
        try {
            staffService.addStaff(name, loginName, password, phone, email, role, deptId, session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            throw new RuntimeException(e);
        } catch (loginErrorException e) {
            throw new RuntimeException(e);
        }catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/updateStaff")
    public ResponseResult updateStaff(String id,String name , String phone , String email, String role , String deptId){
        ResponseResult result =new ResponseResult();

        try {
            staffService.updateStaff(id,name,phone, email, role, deptId);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            throw new RuntimeException(e);
        }catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @RequestMapping("/enableStaff")
    public ResponseResult enableStaff(String id){
        ResponseResult result = new ResponseResult();

        try {
            staffService.enableStaff(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            throw new RuntimeException(e);
        } catch (DeptStatusErrorException e) {
            result.setMessage(e.getMessage());
        }


        return result;
    }

    @RequestMapping("/disableStaff")
    public ResponseResult disableStaff(String id){
        ResponseResult result = new ResponseResult();
        try {
            staffService.disableStaff(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

}
